package com.appcenter.message.service;

import com.appcenter.jz.page.PagedResult;
import com.appcenter.message.input.LoadManageMessageTargetListPageInput;
import com.appcenter.message.input.SearchManageMessageTargetListInput;
import com.appcenter.message.mapper.MessageTargetsMapper;
import com.appcenter.message.mapper.MessagesMapper;
import com.appcenter.message.model.MessageTargets;
import com.appcenter.message.model.Messages;
import com.appcenter.message.output.LoadManageMessageTargetListPageOutput;
import com.appcenter.message.output.SearchManageMessageTargetListOutput;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class MessageTargetsService {

    @Autowired
    MessageTargetsMapper messageTargetsMapper;

    @Autowired
    MessagesMapper messagesMapper;

    /**
     *  创建人:Coke
     *  备注: 加载消息
     *  时间: 2019/4/26 14:51 
     *  参数: 
     */
    public LoadManageMessageTargetListPageOutput LoadManageMessageTargetListPage(LoadManageMessageTargetListPageInput input){
        LoadManageMessageTargetListPageOutput output = new LoadManageMessageTargetListPageOutput();
        SearchManageMessageTargetListOutput curSearchManageMessageTargetListOutput=SearchManageMessageTargetList(input);
        output.setItems(curSearchManageMessageTargetListOutput.getItems());
        output.setMsg(curSearchManageMessageTargetListOutput.getMsg());
        return output;
    }
    /**
     *  创建人:Coke
     *  备注: 搜索消息
     *  时间: 2019/4/26 14:51 
     *  参数: 
     */
    public SearchManageMessageTargetListOutput SearchManageMessageTargetList(SearchManageMessageTargetListInput input){
        SearchManageMessageTargetListOutput output = new SearchManageMessageTargetListOutput();
        Example exampleMsg = new Example(MessageTargets.class);
        Example.Criteria criteriaMsg = exampleMsg.createCriteria();
        criteriaMsg.andEqualTo("messageId",input.getMessageId());
        PageHelper.startPage(input.getPage().getPageNo(),input.getPage().getPageSize(),"createTime desc");
        List<MessageTargets> msgs = messageTargetsMapper.selectByExample(exampleMsg);
        PageInfo<MessageTargets> page = new PageInfo<>(msgs);
        Messages msg = messagesMapper.selectByPrimaryKey(input.getMessageId());
        output.setMsg(msg);
        output.setItems(new PagedResult<MessageTargets>(page.getList(),(int)page.getTotal()));
        return output;
    }
}
